package com.umbacos.udm.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.umbacos.udm.client.arch.Popup;
import com.umbacos.udm.client.model.AsyncComm;
import com.umbacos.udm.client.model.UdmStatus;
import com.umbacos.udm.client.view.TrackerView;

public class TrackerPresenter implements ClickHandler {

	public static enum TrackerType {WGET, CTOR, INFO, AMULE}

	public final String filename;
	public final TrackerType type;

	private boolean isPlaying=false;
	
	private String pid="";

	public TrackerView tv=new TrackerView();
	final private UdmStatus udmStatus;
	
	public TrackerPresenter(UdmStatus udmStatus, final String filename, String pid){
		this.udmStatus=udmStatus;
		this.filename=filename;
		switch (filename.charAt(filename.length()-1)){
		case 't': this.type=TrackerType.WGET; break;
		case 'r': this.type=TrackerType.CTOR; break;
		case 'o': this.type=TrackerType.INFO; break;
		case 'u': this.type=TrackerType.AMULE; break;
		default: this.type=TrackerType.INFO; break;
		}
		this.pid=pid;
		tv.setIcons(type, checkIsRunning());
		tv.ibPlay.addClickHandler(this);
		tv.ibStop.addClickHandler(this);
		tv.ibDele.addClickHandler(this);
		tv.ibMeta.addClickHandler(this);
	}

	public boolean checkIsRunning(){
		this.isPlaying=false;
		if (pid != null)
			if (pid.length()>0) 
				this.isPlaying=true;
		return isPlaying;
	}

	public void setPid(String newpid) {
		this.pid=newpid;
		tv.setIcons(type, checkIsRunning());
	}
	
	@Override
	public void onClick(ClickEvent event) {
		String clicked=event.getRelativeElement().getPropertyString("title");

		if (clicked.startsWith("Show")) {
			final String token=filename.substring(0, filename.lastIndexOf('.'));
			new UdmStatus().getTracker(token, new RequestCallback() {
				
				@Override
				public void onResponseReceived(Request request, Response response) {
					if (200 == response.getStatusCode()) {
						VerticalPanel lbl=new VerticalPanel();
						String [] str=response.getText().split("\n");
						for (int i=0; i<str.length;i++) {
							lbl.add(new Label(str[i]));
							lbl.add(new Label("______________"));
						}
						lbl.setWidth("600px");
						Popup pp = new Popup(filename, lbl, Popup.POPUP_CLOSE);
						pp.showPopup();
					} 
					else {        
						Popup pp = new Popup("Status Update", new HTML(AsyncComm.SERVER_ERROR + 
								"<br>" + response.getStatusCode() +
								" " + response.getStatusText() + " " + token),
								Popup.POPUP_CLOSE);
						pp.showPopup();
					} 
				}

				@Override
				public void onError(Request request, Throwable exception) {
					Popup pp = new Popup("Status Update", AsyncComm.SERVER_ERROR, Popup.POPUP_CLOSE);
					pp.showPopup();
				}
			});
	
		} else if (clicked.startsWith("Delete")) {
			Popup pp0=new Popup("Delete the tracker?", null, Popup.POPUP_OK + Popup.POPUP_CANCEL);
			pp0.setAction(null, "del", filename, "", udmStatus);
			pp0.showPopup();

		} else if (clicked.startsWith("Restart")) { 
			String c=(type==TrackerType.WGET?"startdnl":"starttor");
			Popup pp0=new Popup("Restart the tracker?", null, Popup.POPUP_OK + Popup.POPUP_CANCEL);
			pp0.setAction(null, c, filename, "", udmStatus);
			pp0.showPopup();

		} else if (clicked.startsWith("Pause")) { 
			Popup pp0=new Popup("Pause the tracker?", null, Popup.POPUP_OK + Popup.POPUP_CANCEL);
			pp0.setAction(null, "stop", filename, "", udmStatus);
			pp0.showPopup();
		}
			
	}
}
